package com.mdz.sbgl.Controller;


import cn.hutool.json.XML;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mdz.sbgl.Config.ServerConfig;
import com.mdz.sbgl.Entity.Sbflb;
import com.mdz.sbgl.Entity.Sbwhb;
import com.mdz.sbgl.Entity.Sbxxb;
import com.mdz.sbgl.Entity.Wenhb;
import com.mdz.sbgl.Mapper.SbflbMapper;
import com.mdz.sbgl.Mapper.SbwhbMapper;
import com.mdz.sbgl.Mapper.SbxxbMapper;
import com.mdz.sbgl.Mapper.WenhbMapper;
import com.mdz.sbgl.util.FileUtil;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 孟德志
 * @since 2020-08-12
 */
@Controller
@RequestMapping("/sbgl/sbxxb")
public class SbxxbController {

    @Autowired
    private SbxxbMapper sbxxbMapper;

    @Autowired
    private SbwhbMapper sbwhbMapper;

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private HttpSession session;

    @Autowired
    private SbflbMapper sbflbMapper;

    @Autowired
    private WenhbMapper wenhbMapper;
    /*
     * 显示sbxx页面
     */
    @RequestMapping(value = "sbxxb", method = RequestMethod.GET)
    public String sbxxb(HttpServletRequest request) {
        request.setAttribute("url",serverConfig.geturl());
        return "sbxx";
    }

    /*
     * 显示sbxxb所有数据
     */
    @ResponseBody
    @RequestMapping(value = "/getsbxxb/{gsid}", method = RequestMethod.GET)
    public String getsbxxb(@RequestParam int pageNumber, @RequestParam int pageSize, @PathVariable("gsid") String gsid, HttpServletResponse response) {
        response.setContentType("text/json");
        response.setCharacterEncoding("utf-8");
        PageHelper.startPage(pageNumber, pageSize);
        List<Sbxxb> ncList = sbxxbMapper.selectList(new QueryWrapper<Sbxxb>().eq("gsid", gsid).eq("bfbs", 0));
        PageInfo page = new PageInfo(ncList);
        List<Sbxxb> rows = page.getList();
        int total = (int) page.getTotal();
        JSONObject result = new JSONObject();
        result.put("rows", rows);
        result.put("total", total);
        return result.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "/getsblist", method = RequestMethod.GET)
    public String getsblist(HttpServletResponse response, @RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize, @RequestParam(value = "sbbh", required = false, defaultValue = "") String sbbh, @RequestParam(value = "gsid", required = false, defaultValue = "") String gsid, @RequestParam(value = "bfbs", required = false, defaultValue = "") String bfbs) {
        response.setContentType("text/json");
        response.setCharacterEncoding("utf-8");
        PageHelper.startPage(pageNumber, pageSize);
        List<Sbxxb> ncList = sbxxbMapper.selectList(new QueryWrapper<Sbxxb>().eq("gsid", gsid).and(wrapper -> wrapper.like("sbbh", sbbh).or().like("bm",sbbh).or().like("sbmc",sbbh)).like("bfbs", bfbs));
        PageInfo page = new PageInfo(ncList);
        List<Sbxxb> rows = page.getList();
        int total = (int) page.getTotal();
        JSONObject result = new JSONObject();
        result.put("rows", rows);
        result.put("total", total);
        return result.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "/insertsb", method = RequestMethod.POST)
    public int insertsb(Sbxxb sbxxb) {
        System.out.println(sbxxb);
        String[] sjshoum = sbxxb.getYpdx().split(",");
        if (sbxxb.getYpdx().length() <= 2) {
            sbxxb.setYpdx("");
        } else {
            for (int i = 0; i < sjshoum.length; i++) {
                if (i == 1 && sbxxb.getYpdx().length() > 2) {
                    sbxxb.setYpdx(sjshoum[i - 1] + "+" + sjshoum[i]);
                }
                if (i == 2 && sbxxb.getYpdx().length() > 2) {
                    sbxxb.setYpdx(sjshoum[i - 2] + "+" + sjshoum[i - 1] + "+" + sjshoum[i]);
                }
                if (i == 0 && sbxxb.getYpdx().length() > 2) {
                    sbxxb.setYpdx(sjshoum[i]);
                }
            }
        }
        if (sbxxb.getNcggdx().length()>0){
            sbxxb.setNcggdx(sbxxb.getNcggdx() + " G");
        }
        return sbxxbMapper.insert(sbxxb);
    }

    @ResponseBody
    @RequestMapping(value = "/editspkfk/{sbbh}", method = RequestMethod.PUT)
    public Integer editspkfk(Sbxxb sbxxb) {
        return sbxxbMapper.update(sbxxb, new UpdateWrapper<Sbxxb>().eq("sbbh", sbxxb.getSbbh()));
    }

    @ResponseBody
    @RequestMapping(value = "/getsbwh/{sbbh}/{gsid}", method = RequestMethod.GET)
    public List<Sbxxb> getsbwh(ModelMap map, @PathVariable("sbbh") String sbbh, @PathVariable("gsid") String gsid) {
        List<Sbxxb> sbxxb = sbxxbMapper.selectList(new QueryWrapper<Sbxxb>().like("sbbh", sbbh).eq("gsid", gsid));
        return sbxxb;
    }

    @ResponseBody
    @RequestMapping(value = "/getmaxbh/{sbid}/{gsid}", method = RequestMethod.GET)
    public String getmaxbh(ModelMap map, @PathVariable("sbid") String sbid, @PathVariable("gsid") String gsid) {
        String maxbh = sbxxbMapper.getmaxbh(sbid,gsid);
        return maxbh;
    }

    @RequestMapping("/sbwh/{sbbh}")
    public String sbwh(ModelMap map, @PathVariable("sbbh") String sbbh) {
        List<Sbxxb> sbxxb = sbxxbMapper.selectList(new QueryWrapper<Sbxxb>().like("sbbh", sbbh));
        map.put("sbxxb", sbxxb);
        List<Sbwhb> sbwhb = sbwhbMapper.getsbwh(sbbh);
        map.put("sbwhb", sbwhb);
        return "sbwh";
    }

    @ResponseBody
    @RequestMapping(value = "/getshiyr/{sbbh01}/{gsid}", method = RequestMethod.GET)
    public String getshiyr(@PathVariable("sbbh01") String sbbh01, @PathVariable("gsid") String gsid) {
        List<Sbxxb> sbxxbs = sbxxbMapper.selectList(new QueryWrapper<Sbxxb>().eq("sbbh", sbbh01).eq("gsid", gsid));
        return sbxxbs.get(0).getShiyr();
    }

    @ResponseBody
    @RequestMapping(value = "/getshiyrbysbbh/{sbbh}", method = RequestMethod.GET)
    public String getshiyrbysbbh(@PathVariable("sbbh") String sbbh) {
        List<Sbxxb> sbxxbs = sbxxbMapper.selectList(new QueryWrapper<Sbxxb>().eq("sbbh", sbbh));
        System.out.println(sbxxbs.get(0).getShiyr());
        return sbxxbs.get(0).getShiyr();
    }

    @ResponseBody
    @RequestMapping(value = "/getbmid/{sbbh01}", method = RequestMethod.GET)
    public String getbmid(@PathVariable("sbbh01") String sbbh01) {
        List<Sbxxb> sbxxbs = sbxxbMapper.selectList(new QueryWrapper<Sbxxb>().eq("sbbh", sbbh01));
        return sbxxbs.get(0).getBm();
    }

    @ResponseBody
    @RequestMapping(value = "/updatesbxxbbywh/{sbbh01}", method = RequestMethod.PUT)
    public Integer updatesbxxbbywh(Sbxxb sbxxb, @PathVariable("sbbh01") String sbbh01) {
        sbxxb.setBm(sbxxb.getBm());
        return sbxxbMapper.update(sbxxb, new UpdateWrapper<Sbxxb>().eq("sbbh", sbxxb.getSbbh()));
    }

    @ResponseBody
    @RequestMapping(value = "/updatesb/{sbbh01}", method = RequestMethod.PUT)
    public Integer updatesb(Sbxxb sbxxb, @PathVariable("sbbh01") String sbbh01) {
        String[] ypdx = sbxxb.getYpdx().split(",");
        if (sbxxb.getYpdx().length() <= 2) {
            sbxxb.setYpdx("");
        } else {
            for (int i = 0; i < ypdx.length; i++) {
                if (i == 1 && sbxxb.getYpdx().length() > 2) {
                    sbxxb.setYpdx(ypdx[i - 1] + "+" + ypdx[i]);
                }
                if (i == 2 && sbxxb.getYpdx().length() > 2) {
                    sbxxb.setYpdx(ypdx[i - 2] + "+" + ypdx[i - 1] + "+" + ypdx[i]);
                }
                if (i == 0 && sbxxb.getYpdx().length() > 2) {
                    sbxxb.setYpdx(ypdx[i]);
                }
            }
        }
        if (sbxxb.getNcggdx().contains("G")){
            System.out.println("你输入的内存大小包含G，内存大小为"+sbxxb.getNcggdx());
            sbxxb.setNcggdx(sbxxb.getNcggdx());
        }else {
            System.out.println("你输入的内存不包含G，内存大小为"+sbxxb.getNcggdx());
            sbxxb.setNcggdx(sbxxb.getNcggdx() + " G");
        }
        return sbxxbMapper.update(sbxxb, new UpdateWrapper<Sbxxb>().eq("sbbh", sbxxb.getSbbh()));
    }

    @ResponseBody
    @RequestMapping(value = "/updatesbxxb/{sbbh01}", method = RequestMethod.PUT)
    public Integer updatesbxxb(Sbxxb sbxxb, @PathVariable("sbbh01") String sbbh01) {
        String[] ypdx = sbxxb.getYpdx().split(",");
        if (sbxxb.getYpdx().length() <= 2) {
            sbxxb.setYpdx("");
        } else {
            for (int i = 0; i < ypdx.length; i++) {
                if (i == 1 && sbxxb.getYpdx().length() > 2) {
                    sbxxb.setYpdx(ypdx[i - 1] + "+" + ypdx[i]);
                }
                if (i == 2 && sbxxb.getYpdx().length() > 2) {
                    sbxxb.setYpdx(ypdx[i - 2] + "+" + ypdx[i - 1] + "+" + ypdx[i]);
                }
                if (i == 0 && sbxxb.getYpdx().length() > 2) {
                    sbxxb.setYpdx(ypdx[i]);
                }
            }
        }
        return sbxxbMapper.updatesbxxb(sbxxb);
    }

    @ResponseBody
    @RequestMapping(value = "/updatesbxxbbybf/{sbbh01}", method = RequestMethod.PUT)
    public Integer updatesbxxbbybf(Sbxxb sbxxb, @PathVariable("sbbh01") String sbbh01) {
        sbxxb.setBfbs("1");
        return sbxxbMapper.update(sbxxb, new UpdateWrapper<Sbxxb>().eq("sbbh", sbxxb.getSbbh()));
    }

    @RequestMapping("/exportExcel")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) {
        String gsid=session.getAttribute("gsid").toString();
        System.out.println(gsid);
        List<Sbxxb> sbxxbs = sbxxbMapper.selectList(new QueryWrapper<Sbxxb>().eq("bfbs", "0").eq("gsid",gsid).orderByAsc("sbbh"));
        List<Sbflb> sbflbs = sbflbMapper.selectList(new QueryWrapper<Sbflb>().eq("gsid",gsid));
        HSSFWorkbook workbook = new HSSFWorkbook();
//        从sbflb表中获取所有的sbid和sbmc
        List<String> sheetname = new ArrayList();//excel中工资表的名字
        List<String> listforbh = new ArrayList();//获取编号的值，在后面和比较，看看是否包含
        for (int i = 0; i < sbflbs.size(); i++) {
            listforbh.add(sbflbs.get(i).getSbid());
            sheetname.add(sbflbs.get(i).getSbmc());
        }
//        sheetname.add("汇总");


//        HSSFSheet sheetts = workbook.createSheet("汇总");
////        sheetts.setDefaultColumnWidth((short) 10);
//        HSSFRow rowts = sheetts.createRow(0);
//        String[] headts = {"序号", "设备编号", "设备名称", "规格型号", "主板", "CPU", "内存", "硬盘", "显示器", "单位", "数量", "生产厂商", "购置日期", "IP", "MAC", "备注", "部门", "签字","财务备注"};
//        HSSFCellStyle cellStyle = workbook.createCellStyle();
//        for (short i = 0; i < headts.length; i++) {
//            HSSFCell cell = rowts.createCell(i);
//            cell.setCellValue(headts[i]);
//            cell.setCellStyle(cellStyle);
//        }
//        for (int flbody=0;flbody<sbxxbts.size();flbody++){
//            rowts = sheetts.createRow(flbody + 1);
//            Sbxxb u = sbxxbts.get(flbody);
//            String[] spkfkArray = new String[19];
//            spkfkArray[0] = flbody + 1 + "";
//            spkfkArray[1] = u.getSbbh();
//            spkfkArray[2] = u.getSbmc();
//            spkfkArray[3] = u.getGuige();
//            spkfkArray[4] = u.getZbxh();
//            spkfkArray[5] = u.getCpuxh();
//            spkfkArray[6] = u.getNcggdx();
//            spkfkArray[7] = u.getYpdx();
//            spkfkArray[8] = u.getXsqppcc();
//            spkfkArray[9] = u.getDw();
//            spkfkArray[10] = u.getShul() + "";
//            spkfkArray[11] = u.getShccj();
//            spkfkArray[12] = u.getGmrq();
//            spkfkArray[13] = u.getIpdz();
//            spkfkArray[14] = u.getMacdz();
//            spkfkArray[15] = u.getBeiz();
//            spkfkArray[16] = u.getBm();
//            spkfkArray[17] = u.getShiyr();
//            spkfkArray[18] = u.getCaiwbeiz();
//            for (int iArray = 0; iArray < spkfkArray.length; iArray++) {
//                rowts.createCell(iArray).setCellValue(spkfkArray[iArray]);
//            }
//        }
//

        for (int j=0;j<sheetname.size();j++){
            List<Sbxxb> sbxxbts=new ArrayList<>();//该list为excel显示的表中数据
            for (Sbxxb sbxxb:sbxxbs){
                if (sbxxb.getSbbh().trim().contains(listforbh.get(j))){
                    sbxxbts.add(sbxxb);
                }
            }
            HSSFSheet sheetts = workbook.createSheet(sheetname.get(j));
            sheetts.setDefaultColumnWidth((short) 10);
            HSSFRow rowts = sheetts.createRow(0);
            String[] headts = {"序号", "设备编号", "设备名称", "品牌型号", "主板", "CPU", "内存", "硬盘", "显示器", "单位", "数量", "生产厂商", "购置日期", "IP", "MAC", "备注", "部门", "签字","财务备注"};
            HSSFCellStyle cellStyle = workbook.createCellStyle();
            for (short i = 0; i < headts.length; i++) {
                HSSFCell cell = rowts.createCell(i);
                cell.setCellValue(headts[i]);
                cell.setCellStyle(cellStyle);
            }
            for (int flbody=0;flbody<sbxxbts.size();flbody++){
                rowts = sheetts.createRow(flbody + 1);
                Sbxxb u = sbxxbts.get(flbody);
                String[] spkfkArray = new String[19];
                spkfkArray[0] = flbody + 1 + "";
                spkfkArray[1] = u.getSbbh();
                spkfkArray[2] = u.getSbmc();
                spkfkArray[3] = u.getGuige();
                spkfkArray[4] = u.getZbxh();
                spkfkArray[5] = u.getCpuxh();
                spkfkArray[6] = u.getNcggdx();
                spkfkArray[7] = u.getYpdx();
                spkfkArray[8] = u.getXsqppcc();
                spkfkArray[9] = u.getDw();
                spkfkArray[10] = u.getShul() + "";
                spkfkArray[11] = u.getShccj();
                spkfkArray[12] = u.getGmrq();
                spkfkArray[13] = u.getIpdz();
                spkfkArray[14] = u.getMacdz();
                spkfkArray[15] = u.getBeiz();
                spkfkArray[16] = u.getBm();
                spkfkArray[17] = u.getShiyr();
                spkfkArray[18] = u.getCaiwbeiz();
                for (int iArray = 0; iArray < spkfkArray.length; iArray++) {
                    rowts.createCell(iArray).setCellValue(spkfkArray[iArray]);
                }
            }
        }
        FileUtil.createFile(response, workbook);
    }

    @ResponseBody
    @RequestMapping("/xiu/{id}")
    public List<Sbxxb> xiu(@PathVariable("id") String id,HttpSession session){
        List<Sbxxb> sbxxbs=sbxxbMapper.selectList(new QueryWrapper<Sbxxb>().eq("id",id));
        session.setAttribute("gsid",sbxxbs.get(0).getGsid());
        session.setAttribute("sbmc",sbxxbs.get(0).getSbmc());
        session.setAttribute("sbbh",sbxxbs.get(0).getSbbh());
        session.setAttribute("guige",sbxxbs.get(0).getGuige());
        session.setAttribute("shccj",sbxxbs.get(0).getShccj());
        session.setAttribute("gmrq",sbxxbs.get(0).getGmrq());
        session.setAttribute("zbxh",sbxxbs.get(0).getZbxh());
        session.setAttribute("cpuxh",sbxxbs.get(0).getCpuxh());
        session.setAttribute("xsqppcc",sbxxbs.get(0).getXsqppcc());
        session.setAttribute("xkxh",sbxxbs.get(0).getXkxh());
        session.setAttribute("ncggdx",sbxxbs.get(0).getNcggdx());
        session.setAttribute("xkxh",sbxxbs.get(0).getXkxh());
        session.setAttribute("ncggdx",sbxxbs.get(0).getNcggdx());
        session.setAttribute("ypdx",sbxxbs.get(0).getYpdx());
        session.setAttribute("ipdz",sbxxbs.get(0).getIpdz());
        session.setAttribute("macdz",sbxxbs.get(0).getMacdz());
        session.setAttribute("bm",sbxxbs.get(0).getBm());
        session.setAttribute("shiyr",sbxxbs.get(0).getShiyr());
        session.setAttribute("beiz",sbxxbs.get(0).getBeiz());
        session.setAttribute("sjbeiz",sbxxbs.get(0).getSjbeiz());
        session.setAttribute("bfbeiz",sbxxbs.get(0).getBfbeiz());
        session.setAttribute("xsqgmrq",sbxxbs.get(0).getXsqgmrq());
        return sbxxbs;
    }

    @ResponseBody
    @RequestMapping("/weihu/{sbbh}")
    public List<Sbxxb> weihu(@PathVariable("sbbh") String sbbh, HttpSession session){
        List<Sbxxb> sbxxbs=sbxxbMapper.selectList(new QueryWrapper<Sbxxb>().eq("sbbh",sbbh));
        session.setAttribute("gsid",sbxxbs.get(0).getGsid());
        session.setAttribute("sbmc",sbxxbs.get(0).getSbmc());
        session.setAttribute("sbbh",sbxxbs.get(0).getSbbh());
        session.setAttribute("guige",sbxxbs.get(0).getGuige());
        session.setAttribute("shccj",sbxxbs.get(0).getShccj());
        session.setAttribute("gmrq",sbxxbs.get(0).getGmrq());
        session.setAttribute("zbxh",sbxxbs.get(0).getZbxh());
        session.setAttribute("cpuxh",sbxxbs.get(0).getCpuxh());
        session.setAttribute("xsqppcc",sbxxbs.get(0).getXsqppcc());
        session.setAttribute("xkxh",sbxxbs.get(0).getXkxh());
        session.setAttribute("ncggdx",sbxxbs.get(0).getNcggdx());
        session.setAttribute("xkxh",sbxxbs.get(0).getXkxh());
        session.setAttribute("ncggdx",sbxxbs.get(0).getNcggdx());
        session.setAttribute("ypdx",sbxxbs.get(0).getYpdx());
        session.setAttribute("ipdz",sbxxbs.get(0).getIpdz());
        session.setAttribute("macdz",sbxxbs.get(0).getMacdz());
        session.setAttribute("bm",sbxxbs.get(0).getBm());
        session.setAttribute("shiyr",sbxxbs.get(0).getShiyr());
        session.setAttribute("beiz",sbxxbs.get(0).getBeiz());
        session.setAttribute("sjbeiz",sbxxbs.get(0).getSjbeiz());
        session.setAttribute("bfbeiz",sbxxbs.get(0).getBfbeiz());
        session.setAttribute("xsqgmrq",sbxxbs.get(0).getXsqgmrq());
        return sbxxbs;
    }

    @ResponseBody
    @RequestMapping("/getgsid/{sbbh}")
    public String getgsid(@PathVariable("sbbh") String sbbh){
        List<Sbxxb> sbxxbs=sbxxbMapper.selectList(new QueryWrapper<Sbxxb>().eq("sbbh",sbbh));
        return sbxxbs.get(0).getGsid();
    }

    @ResponseBody
    @RequestMapping("/getbm/{sbbh}")
    public String getbm(@PathVariable("sbbh") String sbbh){
        List<Sbxxb> sbxxbs=sbxxbMapper.selectList(new QueryWrapper<Sbxxb>().eq("sbbh",sbbh));
        return sbxxbs.get(0).getBm();
    }

    @ResponseBody
    @RequestMapping(value = "/updateygxxbbygsid/{gsid01}/{bm01}", method = RequestMethod.PUT)
    public Integer updatesbxxbbygsid(Sbxxb sbxxb, @PathVariable("gsid01") String gsid01, @PathVariable("bm01") String bm01) {
        return sbxxbMapper.update(sbxxb, new UpdateWrapper<Sbxxb>().eq("gsid", gsid01).eq("bm",bm01));
    }

    @ResponseBody
    @RequestMapping(value = "/updatesb/{gsid}/{sbbh}", method = RequestMethod.PUT)
    public Integer updatesb(Sbxxb sbxxb,@PathVariable("gsid") String gsid, @PathVariable("sbbh") String sbbh) {
        sbxxb.setSbmc(sbxxb.getSbmc());
        return sbxxbMapper.updatesb(gsid,sbbh,sbxxb.getSbmc());
    }

    @ResponseBody
    @RequestMapping(value = "/updatesbxxbbyid", method = RequestMethod.PUT)
    public Integer updatesbxxbbyid(Sbxxb sbxxb){
        return sbxxbMapper.updateById(sbxxb);
    }

}

